/*-------------------------------------------------------------------------------
* file:     app_updata.c
* brief:    application
* copyright Copyright (C) 2020-2022 @ NanoIC
* Date:     2022-08-15
* Version:  V1.00
* Author:   JMH
*-------------------------------------------------------------------------------*/
/* include ----------------------------------------------------------------*/
#include "app_upgrade.h"

/* Private macro-----------------------------------------------------------*/
#define UPGRADE_TEMP_BUF_LEN            ((FLASH_PAGE_SIZE+64)/4)

/* externs variablesr------------------------------------------------------*/
extern uint8_t one_wire_temp_ack_buf[ONE_WIRE_BUS_TEMP_ACK_BUF_LEN];
extern uint8_t one_wire_temp_ack_len;

extern OneWireSerialNumber one_wire_app_serial;
extern OneWireSetParaItemData one_wire_pata_item;

/* Private variablesr------------------------------------------------------*/
UpgradeInfo_Typedef upgrade_info;/*升级有关的结构体*/
UpgradeHead_Typedef upgrade_head_info;/*校验升级文件有关*/
volatile uint32_t upgrade_temp_buf[UPGRADE_TEMP_BUF_LEN]; /*升级用的临时数据备份区*/ 
uint32_t header_write_temp_buf[FLASH_PAGE_SIZE/4];
volatile uint32_t upgrade_temp_buf_index;
volatile uint8_t upgrade_firmware_checkok_flag;
volatile uint16_t updata_reset_cnt;
volatile uint8_t upgrade_rst_flag;

volatile bool upgrade_star_flag;

uint8_t global_upgrade_stop_herat_flag;


uint8_t buf[256]={0};
uint32_t app_upgrade_get_self_ver(void);
/* Private function--------------------------------------------------------*/
/**
 * @brief 升级数据结构初始化
 * 
 */
void app_updata_init(void)
{

	uint32_t temp=0;
	app_spi_flash_page_read(FLASH_OTA_BASE_ADDR, (uint8_t*)&temp, 4);
	#if (APP_LOG_ALL_ENABLE == 1)
		printf("temp %x\n",temp);
	#endif
	
	if(temp!=0xffffffff)
	{
		drv_flash_lock_config(UNLOCK);
		boot_flash_unlock_flash(UNLOCK);
		app_iwt_clr();
		
		app_spi_flash_sector_or_block_erase(FLASH_OTA_BASE_ADDR - FLASH_SECTOR_SIZE,FLASH_BLOCK64_SIZE);
		app_iwt_clr();
		drv_flash_lock_config(AREA_00000_2FFFF);
		boot_flash_lock_flash(AREA_00000_2FFFF);
	}

    memset((uint8_t *)&upgrade_info,0,sizeof(UpgradeInfo_Typedef));

    for(uint32_t i=0;i<UPGRADE_TEMP_BUF_LEN;i++)
    {
        upgrade_temp_buf[i] = 0x00;
    }
	app_upgrade_get_self_ver();
    global_upgrade_stop_herat_flag = 0x01;
    upgrade_firmware_checkok_flag = 0;
    upgrade_temp_buf_index = 0x00;
    updata_reset_cnt = 0x00;
    upgrade_rst_flag = 0x00;
    upgrade_star_flag = false;
    #if APP_DEBUG_POWER_ON_LOG_ENABLE
        // printf("keyboard version= 0x%.4x\n",app_upgrade_host_read_version());
    #endif
}

/**
 * @brief 将四字节的uint8_t合并成uint32_t
 * 
 * @param p_char    : 要合并的数据指针
 * @return uint32_t : 合并后的结果
 */
uint32_t app_upgrade_pchar_to_uint(uint8_t *p_char)
{
    uint32_t ui_temp;
    ui_temp = ((uint32_t)p_char[0] 
            + ((uint32_t)p_char[1] << 8)
            + ((uint32_t)p_char[2] << 16)
            + ((uint32_t)p_char[3] << 24));
    return ui_temp;
}

/**
 * @brief 将三字节的uint8_t合并成uint32_t
 * 
 * @param p_char    : 要合并的数据指针
 * @return uint32_t : 合并后的结果
 */
uint32_t app_upgrade_three_byte_to_uint(uint8_t *p_char)
{
    uint32_t ui_temp;
    ui_temp = ((uint32_t)p_char[0] 
            + ((uint32_t)p_char[1] << 8)
            + ((uint32_t)p_char[2] << 16)
            );
    return ui_temp;
}

uint32_t get_two_num_sum(uint32_t one,uint32_t two)
{
	uint32_t temp_sum = 0;
    temp_sum = ((uint8_t)one)
                + ((uint8_t)(one >> 8))
                + ((uint8_t)(one >> 16)) 
                + ((uint8_t)(one >> 24))
                +((uint8_t)two)
                + ((uint8_t)(two >> 8))
                + ((uint8_t)(two >> 16)) 
                + ((uint8_t)(two >> 24));   
	return temp_sum;
}

/**
 * @brief 将二字节的uint8_t合并成uint16_t
 * 
 * @param p_char    : 要合并的数据指针
 * @return uint16_t : 合并后的结果
 */
uint16_t app_upgrade_pchar_to_ushort(uint8_t *p_char)
{
    uint16_t ui_temp;
    ui_temp = ((uint32_t)p_char[0] 
            + ((uint32_t)p_char[1] << 8));
    return ui_temp;
}

/**
 * @brief 读取程序版本和PID
 * 
 * @return uint32_t 
 *         Bit[31..0]: 键盘MCU程序版本
 */
uint32_t app_upgrade_get_self_ver(void)
{
    uint32_t * p_ver;
    uint32_t ret;
	uint8_t temp1,temp2,temp3,temp4;
//	uint8_t rec_len=0;
//	uint8_t version[8] = {'1','.','0','.','0','.','1','4'};
    p_ver = (uint32_t *)(FLASH_APP_BASE_ADDR + FLASH_SECTION_HEADER_ADDR);
    ret = *p_ver;
	temp1 = (ret>>24) &0xff;
	temp2 = (ret>>16) &0xff;
	temp3 = (ret>>8) &0xff;
	temp4 = ret &0xff;
	
	#if APP_DEBUG_POWER_ON_LOG_ENABLE
		printf("ret  = %2x %2x %2x %2x\n",temp1,temp2,temp3,temp4);
    #endif

	version_vel[0]=temp1+0x30;
	version_vel[2]=temp2+0x30;
	version_vel[4]=temp3+0x30;
	if(temp4/0x10)
	{
		version_vel[6]=(temp4/0x10)+0x30;
		version_vel[7]=(temp4%0x10)+0x30;
		version_vel_size=8;
	}else{
		version_vel[6]=(temp4%0x10)+0x30;
		version_vel_size=7;
	}

	#if APP_DEBUG_POWER_ON_LOG_ENABLE
        printf("version_vel = ");
		console_log_num_byte(version_vel,version_vel_size);
    #endif

    return ret;
}

/**
 * @brief 读取键盘型号
 * 
 * @return uint8_t 0x00 vivo 0x01 iQOO
 */
uint8_t app_upgrade_get_keyboard_type(void)
{
    return sn_iQOO_or_V3pro;
}

/**
 * @brief 读取程序类型
 * 
 * @return uint32_t 
 *         Bit[7..0]: 键盘型号
 *         Bit[11..8]: G-Sensor型号
 *         Bit[14..12]: 键盘NFC型号 
 *         Bit[19..15]：触模板型号
 */
uint32_t app_upgrade_get_self_type(void)
{
    uint32_t * p_ver;
    uint32_t ret;

    p_ver = (uint32_t *)(FLASH_APP_BASE_ADDR + FLASH_SECTION_HEADER_ADDR + 0x08);
    ret = *p_ver;

    return ret;
}

/**
 * @brief 读取设备版本号，一般指触摸板
 * 
 * @return uint32_t 
 *         Bit[23..0]: 设备版本号
 */
uint32_t app_upgrade_get_device_version(void)
{
    uint32_t dev_version;

    dev_version = TP_version&0xffff;
    return (dev_version & 0xffffff);
}

/**
 * @brief 读取键盘颜色
 * 
 * @return uint8_t 
 */
uint8_t app_upgrade_get_self_colour(void)
{
    return 0x40;
}

/**
 * @brief 读取键盘语言
 * 
 * @return uint8_t 
 */
uint8_t app_upgrade_get_self_language(void)
{
    return 0x43;
}
